import '../../assets/js/LodopFuncs.js'
import { mapGetters, mapMutations } from 'vuex'
import {deepCopyObjArray, mileSecondToDate, minutesToHowLong} from '../../assets/js/help.js'
export default {
  data () {
    return {
      isPrintNext: false,
      printInfo: []
    }
  },
  computed: {
    ...mapGetters({
      isDialog: 'getIsDialog'
    })
  },
  methods: {
    ...mapMutations({
      dialogToggle: 'DIALOG_TOGGLE',
      setDialog: 'SET_DIALOG'
    }),
    initLodop () {
      if(!LODOP){
        try {
          window.LODOP = getLodop();
        } catch (err) {
          this.showToast("未检测到打印控件，请安装后重试！",2,2000);
          return;
        }
      }
      LODOP.SET_LICENSES("广东艾科智泊科技股份有限公司","7B9558F5C273EE5613F00ACD6A37E863","廣東艾科智泊科技股份有限公司","C02669B182EBA6EA0DF41B46F6287434");
    },
    toPrintTable () {
      this.initLodop();
      if(LODOP) {
        let _data = deepCopyObjArray(this.tbodysInfo),
            _heads = deepCopyObjArray(this.theadShowed),
            _headsInfo = [],
            _headsProp = [],
            _printInfo = [];
        _headsProp = _heads.map(function(x) {
          return x.prop;
        })
        _headsInfo = _heads.map(function(x) {
          return x.text;
        }.bind(this))

        _printInfo = _data.map(function(x, index) {
          let _obj = {};
          for(let i = 0, len = _headsProp.length; i < len; i++) {
            let key = _headsProp[i];
            if(key=='reno') {
              _obj[key] = index + 1;
            } else {
              let value = x[key];
              if(!value) {
                value = '';
              } else {
                if(['enterTime', 'leaveTime', 'correctTime', 'modTime', 'createTime', 'reportTime', 'lastCommunicationTime', 'deviceUpdateTime', 'parkingSpaceUpdatetime', 'operateTime', 'deviceUpdatetime'].indexOf(key) > -1) {
                  value = mileSecondToDate(value);
                } else if(['stoppingTime'].indexOf(key) > -1) {
                  value = minutesToHowLong(value);
                }
                if(value == 'NO') {value = '否'}else if(value == 'YES') {value = '是'}
              }
              
              _obj[key] = value;
            }
          }
          return _obj;
        })
        this.printInfo = _printInfo;
        this.createPrintTable(_printInfo, _headsInfo);
        let self = this;
        setTimeout(function(){
          this.showDialog('是否继续打印下一页', [], function(){
            self.isPrintNext = true;
            self.$emit('getNextPage', '+');
            setTimeout(function(){
              this.isPrintNext = false;
            }.bind(self), 2000)
          })
        }.bind(self), 1000);
        this.isPrintNext = false;
      }   
      
    },
    createPrintTable (info, theads) {
      let tab = this.$store.state.tab;
      let ThisTab = '';
      if(tab && tab.list && tab.list.length > 0) {
        ThisTab = tab.list[tab.active];
      }
      let title = ThisTab.name;
      let html = '', _title = '';
      _title += '<h3 align=center><STRONG><font color="#0000FF">'+title+'</font></STRONG></h3>';
      html += '<TABLE border=0 cellSpacing=0 cellPadding=0 width="100%">';
      html += '<THEAD>';
      for(let j = 0, _len = theads.length; j < _len; j++) {
        html += '<TH>'+theads[j]+'</TH>';
      }
      html += '</THEAD><TBODY>';
      
      for(let i = 0, len = info.length; i < len; i++) {
        html += '<TR>';
        for(let key in info[i]) {
          var width = '';
          if(key.indexOf('Image') > -1) {
            width = '100px';
          }
          html += '<TD width="'+width+'">'+info[i][key]+'</TD>';
        }
        html += '</TR>';
      }
      html += '</TBODY></TABLE>';
      
      $('#printingTable').empty().append(html);
      // 
      LODOP.PRINT_INIT(title);
      var strStyle="<style>h2{text-align:center;} table,td,th {padding-top: 5px;padding-bottom: 5px;border-width: 1px;border-style: solid;border-collapse: collapse}table,td {padding: 5px;word-break:break-all;}</style>";
      
      // 
      LODOP.ADD_PRINT_TABLE('4%',"3%","94%",'BottomMargin:14mm',strStyle+document.getElementById("printingTable").innerHTML);
      
      LODOP.ADD_PRINT_HTM('1%',"3","94%",30,_title);
		  LODOP.SET_PRINT_STYLEA(0,"FontColor","#FF0000");
      LODOP.SET_PRINT_STYLEA(0,"ItemType",1);

      LODOP.ADD_PRINT_HTM('97%','3%',"94%",28,"<div align='right'><span tdata='pageNO'>##</span>&nbsp;/<span tdata='pageCount'>##</span><div>");
      LODOP.SET_PRINT_STYLEA(0,"ItemType",1);
      LODOP.SET_PRINT_STYLEA(0,"FontSize",12);
      LODOP.PREVIEW();
      // LODOP.PRINT();
    }
  }
}